package com.egame.manage.biz.server.pu;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.ManageClientExt;

import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.ext.op.AdvDetail;
import cn.egame.interfaces.gc.GameInfo;
import cn.egame.interfaces.pu.AppParameter;

public class StoreSortServiceDao extends BaseDao {

	public StoreSortServiceDao() throws ExceptionCommonBase {
		super("egame_game");
	}
	
	public int deleteSort(int activityId)throws ExceptionCommonBase{
		String sql = "update t_parameter_app set enable = '0' where id = " + activityId;
		return this.executeUpdate(sql);
	}
	
	public int upper(int appId, long loginUserId, int activityId) throws RemoteException {
		AppParameter bean = ManageClient.getInstance().getAppParameterById(appId, loginUserId, activityId);
		if(bean != null){
			AppParameter upper = this.getUpperOperationAdvertiseInfo(appId, loginUserId, bean.getId(), bean.getParentId(), bean.getSort());
			if(upper != null){
				this.updateSortNo(appId, loginUserId, bean.getId(), upper.getSort());
				this.updateSortNo(appId, loginUserId, upper.getId(), bean.getSort());
				return 1;
			}
		}
		return -1;
	}
	
	public int lower(int appId, long loginUserId, int activityId) throws RemoteException {
		AppParameter bean = ManageClient.getInstance().getAppParameterById(appId, loginUserId, activityId);
		if(bean != null){
			AppParameter lower = this.getLowerOperationAdvertiseInfo(appId, loginUserId, bean.getParentId(), bean.getSort());
			if(lower != null){
				this.updateSortNo(appId, loginUserId, bean.getId(), lower.getSort());
				this.updateSortNo(appId, loginUserId, lower.getId(), bean.getSort());
				return 1;
			}
		}
		return -1;
	}
	
	public AppParameter getUpperOperationAdvertiseInfo(int appId, long loginUserId, int activityId, int parentId,int sortNo)throws RemoteException{
		String sql = "select id from t_parameter_app" +
				" where parent_id =  " + parentId +
				" and sort >  " + sortNo +
				" ORDER BY sort asc LIMIT 1";
		int id = this.getInt(sql);
		System.out.println(sql);
		return ManageClient.getInstance().getAppParameterById(appId, loginUserId, id);
	}
	
	public AppParameter getLowerOperationAdvertiseInfo(int appId, long loginUserId, int parentId,int sortNo)throws RemoteException{
		String sql = "select id from t_parameter_app" +
				" where parent_id =  " + parentId +
				" and sort <  " + sortNo +
				" ORDER BY sort desc LIMIT 1";
		int id = this.getInt(sql);
		return ManageClient.getInstance().getAppParameterById(appId, loginUserId, id);
	}
	
	public int updateSortNo(int appId, long loginUserId, int activityId,int sortNo)throws ExceptionCommonBase{
		String sql = "update t_parameter_app set sort = " + sortNo + " where id = " + activityId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}
	
	public int getMaxSortNo(int parentId)throws RemoteException{
		String sql = "select max(sort) from t_parameter_app" +
				" where parent_id =  " + parentId +
				" and status = 1";
		return this.getInt(sql);
	}
	
	public List<Integer> topicHistoryIds(int parentId) throws ExceptionCommonBase {
		String sql = "select id  FROM t_parameter_app where parent_id = "
				+ parentId + " and enable =0 order by sort desc";
		return this.getListInteger(sql);
	}

	public List<Integer> listAppParameterByBean(AppParameter bean)throws RemoteException{		
		String sql = "select id from t_parameter_app where enable=1 and 1=1 ";
		if(bean != null){
			if(bean.getParentId()>0){
				sql += " and parent_id = " + bean.getParentId();
			}
			if(bean.getBeginTime() != null){
					sql += " and begin_time >=" + SqlUtils.QuataStr(Utils.toDateString(bean.getBeginTime(), "yyyy-MM-dd HH:mm:ss"));
			}
			if(bean.getEndTime() != null){
				sql += " and end_time <=" + SqlUtils.QuataStr(Utils.toDateString(bean.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
			}
			if(!Utils.stringIsNullOrEmpty(bean.getName())){
				sql += " and name like " + SqlUtils.QuataLikeStr(bean.getName());
			}
		}
		sql += " order by sort DESC";
		return this.getListInteger(sql);
	}
}
